//
//  CCNetWorkTool.swift
//  Swiift_SinaWeibo
//
//  Created by bear on 16/5/22.
//  Copyright © 2016年 bear. All rights reserved.
//

import UIKit
import AFNetworking




enum HttpMethod {
    case GET
    case POST
}

class CCNetWorkTool: NSObject {
    
    
    
    let baseUrl:String?
    
    
    static let shareTool:CCNetWorkTool = {
        
        let tool = CCNetWorkTool(baseURL: nil)
        
        
        return tool;
        
    }()
    
    
    func requset(method:HttpMethod,urlStr:String,parameters:[String:AnyObject]?,finishedBlock:(respondObject:AnyObject?,error:NSError?) -> ()) {
        
    
        let manager = AFHTTPSessionManager();
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
  
        
        if method == .POST {
            manager.POST(urlStr, parameters: parameters, progress: nil, success: { (task, respondObject) in
                
                
                finishedBlock(respondObject: respondObject, error: nil);
                
                }, failure: { (task, error) in
                    
                    finishedBlock(respondObject: nil, error: error)
                    
                    
            })
            
        }else if method == .GET{
            
            
            manager.GET(urlStr, parameters: parameters, progress: nil,  success: { (task, respondObject) in
                
                
                
                finishedBlock(respondObject: respondObject, error: nil);
                
                }, failure: { (task, error) in
                    
                    finishedBlock(respondObject: nil, error: error)
                    
                    
            })
            
        }
        
        
        
        
        
    }
    
     init(baseURL:String?) {
        
        baseUrl=baseURL;
        super.init();
    }

}
